﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;


namespace QGZX.Logic.Admin
{
    public class ArticleController : Controller
    {
        #region 发布文章页面 + ActionResult Release()
        /// <summary>
        /// 发布文章页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Release()
        {
            return View();
        } 
        #endregion

        #region 发布文章提交后的处理 + Release(MODEL.qgzx_article model)
        /// <summary>
        /// 发布文章提交后的处理 + Release(MODEL.qgzx_article model)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Release(MODEL.qgzx_article model)
        {
            model.AdminId = OperateContext.Current.Admin.Id;
            model.Author = OperateContext.Current.Admin.LoginName;
            int res = OperateContext.Current.BLLSession.Iqgzx_articleBLL.Add(model);
            if (res > 0)
            {
                return OperateContext.Current.RedirectAjax("ok", "发布文章成功，请等待审核", null, "");
            }
            return OperateContext.Current.RedirectAjax("err", "发布文章失败", null, "");
        } 
        #endregion
        public ActionResult Index()
        {
            return View();
        }

        #region 获取文章列表数据 +GetArticleData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取文章列表数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetArticleData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_articleBLL.GetPagedList(pageIndex, pageSize, x => x.ArticleId != null, m => m.ArticleId).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_articleBLL.GetListBy(x => x.ArticleId != null).Count();
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改文章 window 的html + EditArticle()
        /// <summary>
        /// 加载修改学生 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditArticle(int id)
        {
            //根据id 查询要修改的文章
            var model = OperateContext.Current.BLLSession.Iqgzx_articleBLL.GetListBy(x => x.ArticleId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改文章信息 EditArticle(MODEL.qgzx_article model)
        /// <summary>
        ///post修改学生信息 EditStudent(MODEL.qgzx_student model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        [ValidateInput(false)]
        public ActionResult EditArticle(MODEL.qgzx_article model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_articleBLL.Modify(model, "Title","Author","Content");
            if (res > 0)
            {
                return Redirect("/admin/Article/index");
            }
            return Redirect("/admin/Article/index");
        }
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string ArticleId = Request.QueryString["ArticleId"];
            string Author = Request.QueryString["Author"];
            string CreateTime = Request.QueryString["CreateTime"];
            string IsAudit = Request.QueryString["IsAudit"];
            string ColumnId = Request.QueryString["ColumnId"];
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_articleBLL.GetListBy(x => x.ArticleId != null).ToList().Select(x => x.ToPOCO());
            if (ArticleId != "")
            {
                list = list.Where(x => x.ArticleId.ToString().Contains(ArticleId)).ToList();
            }
            if (Author != "")
            {
                list = list.Where(x => x.Author.Contains(Author)).ToList();
            }
            if (CreateTime != "")
            {
                list = list.Where(x => x.CreateTime.ToString().Contains(CreateTime)).ToList();
            }
            if (IsAudit != "")
            {
                list = list.Where(x => x.IsAudit.ToString().Contains(IsAudit)).ToList();
            }
            if (ColumnId != "")
            {
                list = list.Where(x => x.ColumnId.ToString().Contains(ColumnId)).ToList();
            }
            
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion
    }
}
